read<-function(){
  data = data.frame(row.names = c("TCGA","比率UP","比率DOWN",1,2,3,4,5,6))
  files = dir(full.names = TRUE)
  for (i in 1:length(files)) {
    path1 = files[i]
    path2 = files[i+1]
    i=i+1
    num = ComparativeGeneProbe(path1,path2)
    data = rbind(data,c(substr(files[i],3,11),num))
  }
  rownames(data) = c("项目名","DOWN重复率","UP重复率","DOWN重复数","edgeR_DOWN","DESeq_DOWN","UP重复数","edgeR_UP","DESeq_UP")
  write.csv(data,"../总文件.csv")
}


ComparativeGeneProbe <- function(path_1 , path_2){
  
  # 读取文件并进行格式化
  # DESeq2 edgeR

  tabele_1 = read.csv(path_1)
  
  tabele_2 = read.csv(path_2)
  
  rownames(tabele_1) = tabele_1[,1]
  
  rownames(tabele_2) = tabele_2[,1]

  DOWN1 = tabele_1[tabele_1$change == "DOWN",]
  
  UP1 = tabele_1[tabele_1$change == "UP",]
  
  NOT1 = tabele_1[tabele_1$change == "NOT",]
  
  DOWN2 = tabele_2[tabele_2$change == "DOWN",]
  
  UP2 = tabele_2[tabele_2$change == "UP",]
  
  NOT2 = tabele_2[tabele_2$change == "NOT",]

  # 使用merge函数完成合并
  #merge(x,y,by,all)函数有很多参数，看起来非常吓人。但他们都几中类型参数有关：
  
  table_DOWN = merge(DOWN1,DOWN2,by = "X", all = FALSE)
  print(paste0("DOWN重复: ",dim(table_DOWN)[1]))
  table_down_all = merge(DOWN1,DOWN2,by = "X", all = TRUE)
  DOWN_rate = 100*dim(table_DOWN)[1]/dim(table_down_all)[1]
  print(paste0("重复率: ",DOWN_rate,"%"))
  
  table_UP = merge(UP1,UP2,by = "X", all = FALSE)
  print(paste0("UP重复了",dim(table_UP)[1]))
  table_up_all = merge(UP1,UP2,by = "X", all = TRUE)
  UP_rate = 100*dim(table_UP)[1]/dim(table_up_all)[1]
  print(paste0("重复率: ",UP_rate,"%"))
  
  ### 输出目录
  table_all = rbind(table_down_all,table_up_all)
  table = rbind(table_DOWN,table_UP)
  
  write.csv(table_all,file = paste0("../",substr(path_1,3,11),"重复性分析结果_ALL.csv"))
  
  write.csv(table,file = paste0("../",substr(path_1,3,11),"重复性分析结果.csv"))
  
  # 返回顺序
  # DOWN重复率 UP重复率 DOWN重复数  edgeR_DOWN DESeq_DOWN UP重复数  edgeR_UP DESeq_UP

  c(DOWN_rate,UP_rate,dim(table_DOWN)[1],dim(DOWN2)[1],dim(DOWN1)[1],dim(table_UP)[1],dim(UP2)[1],dim(UP1)[1])
}


LOgFCAndPvalue<-function(){

  data = data.frame(row.names = c("TCGA","比率UP","NN","比率DOWN","1","2","3"))
  
  print("输入logfc值")
  
  logfc = readline()
  
  print("输入P值")
  
  pvalue = readline()
  
  files = dir(full.names = TRUE)
  
   list = seq(1,length(files),2)
  
  for (i in list) {
  
    path1 = files[i]
  
    path2 = files[i+1]
  
    num = LogFC(path1,path2,logfc,pvalue)
  
    data = rbind(data,c(substr(files[i],3,11),num))
  }
  write.csv(data,"../总文件.csv")
}

LogFC <- function(path_1 , path_2,logfc,p){
  
  # 读取文件并进行格式化
  tabele_1 = read.csv(path_1)
  
  tabele_2 = read.csv(path_2)
  
  rownames(tabele_1) = tabele_1[,1]
  
  
  rownames(tabele_2) = tabele_2[,1]
  
  data1 = tabele_1[tabele_1$pvalue < p & tabele_1$log2FoldChange > logfc,]
  data2 = tabele_1[tabele_1$pvalue > p,]
  data3 = tabele_1[tabele_1$pvalue < p & tabele_1$log2FoldChange < as.numeric(-1 * abs(as.numeric(logfc))),]
  
  if(dim(data1)[1] !=0)data1$change <- "UP"
  if(dim(data2)[1] !=0)data2$change <- "NOT"
  if(dim(data3)[1] !=0)data3$change <- "DOWN"
  
  data1_2 = tabele_2[tabele_2$PValue < p & tabele_2$logFC > logfc,]
  data2_2 = tabele_2[tabele_2$PValue > p,]
  data3_2 = tabele_2[tabele_2$PValue < p & tabele_2$logFC < as.numeric(-1 * abs(as.numeric(logfc))),]
  
  if(dim(data1_2)[1] !=0)data1_2$change <- "UP"
  if(dim(data2_2)[1] !=0)data2_2$change <- "NOT"
  if(dim(data3_2)[1] !=0)data3_2$change <- "DOWN"
  
  data_rbind =rbind(data1,data2,data3)
  write.csv(data_rbind , file = paste0("../",substr(path_1,3,11),"-DESeq","__p=",p,"__loFC=",logfc,".csv"))
  data_rbind2 =rbind(data1_2,data2_2,data3_2)
  write.csv(data_rbind2 , file = paste0("../",substr(path_1,3,11),"-edgeR","__p=",p,"__loFC=",logfc,".csv"))
  
  c(dim(data1)[1],dim(data2)[1],dim(data3)[1],dim(data1_2)[1],dim(data2_2)[1],dim(data3_2)[1])
  
  
}

conbind <- function(){

}